package com.sgzn.sgpay.service.tcp.impl;


import com.sgzn.sgpay.enums.AppName;
import com.sgzn.sgpay.model.request.SgsCreditIncreaseCallBackRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.service.SgsCreditIncreaseCallBackService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import com.sgzn.sgpay.service.tcp.SgTcpProtocolHead;
import com.sgzn.sgpay.service.tcp.SgTcpServer;
import com.sgzn.sgpay.service.tcp.api.TcpConsumeService;
import com.sgzn.sgpay.service.tcp.model.TcpConsumeRequest;
import com.sgzn.sgpay.service.tcp.model.TcpConsumeResponse;
import com.sgzn.sgpay.service.tcp.util.DateUtil;
import com.sgzn.sgpay.util.MoneyUtil;
import com.sgzn.sgpay.util.SerialNoUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.net.Socket;

/**
 * Created by xxjs-gd-llf
 * DATETIME:2018/4/18 16:47
 * Description:
 */
@Deprecated
@Slf4j
@Service
public class TcpConsumeServiceImpl implements TcpConsumeService {

    /**
     * 发起上分消费请求
     * @param request
     */
    @Override
    public SgsResultResponse<String> tcpSendConsumeRequest(TcpConsumeRequest request){
        Socket socket =  SgTcpServer.socketList.get(request.getDeviceId());
        if (socket == null){
            log.info("设备 {} 连接异常，请检查网络",request.getDeviceId());
            return null;
        }
        request.setBeginCode( SgTcpProtocolHead.TCP_PROTOCOL_HEAD);
        request.setCommandCode(SgTcpProtocolHead.TCP_CONSUME_REQUEST );
        request.setCheckSumCode(SgTcpProtocolHead.DEFAULT_PROTOCOL_CHECKSUM );
        request.setEndCode(SgTcpProtocolHead.TCP_PROTOCOL_END);
       String sendBuffer = request.toString() ;
       log.info("上分请求报文 {}" , sendBuffer );
        try {
            socket.getOutputStream().write(sendBuffer.getBytes("UTF-8"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return SgsResponseUtil.INSTANCE.success("发送上分请求成功");
    }


    /**
     * 接收设备端充值成功的应答
     */
   @Override
   public boolean handleConsumeResponse(String recvBuffer, Socket socket){
       boolean consumeSuccess = true;
//       TcpConsumeResponse tcpConsumeResponse = decodeIncreaseackBuffer(recvBuffer);
//       SgsCreditIncreaseCallBackRequest sgsCreditIncreaseCallBackRequest = new SgsCreditIncreaseCallBackRequest();
//
//       String initiationID = SerialNoUtil.generateSerialNo(AppName.SGS);
//       MDC.put("initiationID", initiationID);
//
//       sgsCreditIncreaseCallBackRequest.setInitiationID(initiationID);
//       sgsCreditIncreaseCallBackRequest.setReqSeq(tcpConsumeResponse.getReqSeq());
//       sgsCreditIncreaseCallBackRequest.setRealAmount(tcpConsumeResponse.getActualAmount());
//       sgsCreditIncreaseCallBackRequest.setReqTime(DateUtil.getCurrentDate());
//       sgsCreditIncreaseCallBackRequest.setLoginChannel(AppName.SGS.getCode());
//
//       log.info("调用上分回调接口 ");
//       try {
//           SgTcpServer.sgsCreditIncreaseCallBackService.invoke(sgsCreditIncreaseCallBackRequest);
//       }catch (Exception e){
//           log.error("调用上分回调接口失败",e);
//       }


       return  consumeSuccess;
   }


    /**
     * 解析收到的数据
     * @param recvBuffer
     * @return
     */
    private TcpConsumeResponse decodeIncreaseackBuffer(String recvBuffer){
        TcpConsumeResponse tcpConsumeResponse = new TcpConsumeResponse();

        //命令代码
        tcpConsumeResponse.setCommandCode(recvBuffer.split(SgTcpProtocolHead.TCP_PROTOCOL_SEPERATOR)[1] );


        //设备ID
        tcpConsumeResponse.setDeviceId( recvBuffer.split(SgTcpProtocolHead.TCP_PROTOCOL_SEPERATOR)[2] );

        //流水ID
        tcpConsumeResponse.setReqSeq(recvBuffer.split(SgTcpProtocolHead.TCP_PROTOCOL_SEPERATOR)[3]);

        //上分实际金额
        tcpConsumeResponse.setActualAmount(recvBuffer.split(SgTcpProtocolHead.TCP_PROTOCOL_SEPERATOR)[4]);
        //校验和
        tcpConsumeResponse.setCheckSumCode(recvBuffer.split(SgTcpProtocolHead.TCP_PROTOCOL_SEPERATOR)[5]);

        //校验和检查
        if( ! tcpConsumeResponse.getCheckSumCode().equals(SgTcpProtocolHead.DEFAULT_PROTOCOL_CHECKSUM) ){
            log.info("checksum is wrong.{},{}",tcpConsumeResponse.getCheckSumCode(),SgTcpProtocolHead.DEFAULT_PROTOCOL_CHECKSUM);
            return  null;
        }
        log.info("Receive Client Response {} ",tcpConsumeResponse.toString());
        return  tcpConsumeResponse;
    }


}
